package xin.ctkqiang;

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

import xin.ctkqiang.config.Configuration;
import xin.ctkqiang.controller.ExploitDbController;
import xin.ctkqiang.controller.UserUtil;
import xin.ctkqiang.dto.Database;

public class Main {
    public static final String VERSION = "1.0";
    public static final String TITLE = "ExploitDB 搜索工具";
    public static final String AUTHOR = "钟智强";
    public static final String EMAIL = "ctkqiang@dingtalk.com";
    public static final String GITHUB = "https://github.com/ctkqiang";
    public static final String GITCODE = "https://gitcode.com/ctkqiang_sr";
    public static final String DESCRIPTION = "ExploitDB 搜索工具";
    public static final String USAGE = "mvn clean compile exec:java  或 java -jar {FILENAME}.jar";

    public static final Scanner scanner = new Scanner(System.in);

    public static final ExploitDbController exploitDbController = new ExploitDbController();
    public static final UserUtil userUtil = new UserUtil(Main.scanner);

    private static String UserPrefferedExportExtension;

    private static List<String> allowed = Arrays.asList("csv", "json", "sql");

    private static void PrintBanner() {
        System.out.println();
        System.out.println("╭─────────────────────────────────────────────────────────────────────────╮");
        System.out.println("│  (๑˃̵ᴗ˂̵)و 欢迎来到灵儿的安全世界呀～♡ (づ｡◕‿‿◕｡)づ ");
        System.out.println("│                                                                    ");
        System.out.println("│  工具名称：" + TITLE + " v" + VERSION);
        System.out.println("│  作者昵称：" + AUTHOR + " (๑•̀ㅂ•́)و✧");
        System.out.println("│  简介说明：" + DESCRIPTION);
        System.out.println("│                                                                    ");
        System.out.println("│  📧 邮箱联系: " + EMAIL);
        System.out.println("│  💻 GitHub  : " + GITHUB);
        System.out.println("│  📂 GitCode : " + GITCODE);
        System.out.println("│                                                                    ");
        System.out.println("│  🛠️  使用方法: " + USAGE);
        System.out.println("│                                                                    ");
        System.out.println("│  💌 小声明哟～我是乖乖的好孩子，这工具是帮大家做好事的～         ");
        System.out.println("│  不是让坏蛋乱用的哦 (ง •̀_•́)ง                                    ");
        System.out.println("│  如果有人坏坏地用了，后果自负哒～我不背锅哦～请保持正义感～♡       ");
        System.out.println("╰─────────────────────────────────────────────────────────────────────────╯");
        System.out.println();
    }

    public static void main(String[] args) {
        try {
            Main.PrintBanner();

            /**
             * 嘻嘻～这里是数据库模式设置哦 ♡
             * 你可以从 {@link xin.ctkqiang.dto.Database Database} 枚举里选一个最爱滴数据库：
             * <ul>
             * <li>{@link xin.ctkqiang.dto.Database#MYSQL MYSQL} ：咱们平时用的 MySQL
             * 数据库，默认就选它啦～</li>
             * <li>{@link xin.ctkqiang.dto.Database#SQLITE SQLITE} ：轻巧灵活的 SQLite
             * 嵌入式数据库，萌萌哒！</li>
             * </ul>
             *
             * 小提示✨：默认模式是 MYSQL 哦，要用 SQLite 的话，记得把下面的代码改成
             * <code>Configuration.setDatabaseMode(Database.SQLITE);</code> 才能生效呢～
             *
             * 举个栗子给你看👇：
             * 
             * 乖乖用【默认】 MySQL 嘛 ~
             * 
             * <pre>
             * Configuration.setDatabaseMode(Database.MYSQL);
             * </pre>
             * 
             * 想用 SQLite ~
             * 
             * <pre>
             * Configuration.setDatabaseMode(Database.SQLITE);
             * </pre>
             *
             * 嘻嘻，这样就能轻轻松松切换数据库, 你棒棒啦！🎀🌸💖
             */
            Configuration.setDatabaseMode(Database.MYSQL);

            int pages = userUtil.askForPositiveInt("📄 想爬多少页咧～？【建议填个50以上更爽！】");
            boolean isExport = userUtil.askForYesNo("💾 要不要顺便导出数据嘞？（支持 .csv/.json/.sql/.yaml哦）✨：");

            if (isExport) {
                String extension = userUtil.askForNonEmptyString("📦 你想导出成什么格式呢？（json / sql / csv / yaml / 默认：csv）：");

                if (!(Main.allowed.contains(extension))) {
                    System.out.println("❌ 不支持的格式，已默认使用 csv");
                    Main.UserPrefferedExportExtension = "csv";
                }

                Main.UserPrefferedExportExtension = extension;
            }

            Main.exploitDbController.CreateTableIfNotExists();
            Main.exploitDbController.Crawl(
                    pages,
                    isExport,
                    Main.UserPrefferedExportExtension);
        } catch (Exception e) {
            System.out.println("发生错误：" + e.getMessage());
        } finally {
            Main.scanner.close();
        }
    }
}